Conditions | 14 |
Paths | 22 |
Total Lines | 73 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like calculator.js ➔ f_calc often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | calc_array = new Array(); |
||
8 | function f_calc(id,n) |
||
9 | { |
||
10 | if(n=='ce') |
||
11 | { |
||
12 | init_calc(id); |
||
13 | } |
||
14 | else if(n=='=') |
||
15 | { |
||
16 | if(calc_array[id][0]!='=' && calc_array[id][1]!=1) |
||
17 | { |
||
18 | eval('calcul='+calc_array[id][2]+calc_array[id][0]+calc_array[id][3]+';'); |
||
19 | calc_array[id][0] = '='; |
||
20 | $id(id+'_result').value=calcul; |
||
21 | calc_array[id][2]=calcul; |
||
22 | calc_array[id][3]=0; |
||
23 | } |
||
24 | } |
||
25 | else if(n=='+-') |
||
26 | { |
||
27 | $id(id+'_result').value=$id(id+'_result').value*(-1); |
||
28 | if(calc_array[id][0]=='=') |
||
29 | { |
||
30 | calc_array[id][2] = $id(id+'_result').value; |
||
31 | calc_array[id][3] = 0; |
||
32 | } |
||
33 | else |
||
34 | { |
||
35 | calc_array[id][3] = $id(id+'_result').value; |
||
36 | } |
||
37 | pas_ch = 1; |
||
38 | } |
||
39 | else if(n=='nbs') |
||
40 | { |
||
41 | if($id(id+'_result').value<10 && $id(id+'_result').value>-10) |
||
42 | { |
||
43 | $id(id+'_result').value=''; |
||
44 | } |
||
45 | else |
||
46 | { |
||
47 | $id(id+'_result').value=$id(id+'_result').value.slice(0,$id(id+'_result').value.length-1); |
||
48 | } |
||
49 | if(calc_array[id][0]=='=') |
||
50 | { |
||
51 | calc_array[id][2] = $id(id+'_result').value; |
||
52 | calc_array[id][3] = 0; |
||
53 | } |
||
54 | else |
||
55 | { |
||
56 | calc_array[id][3] = $id(id+'_result').value; |
||
57 | } |
||
58 | } |
||
59 | else |
||
60 | { |
||
61 | if(calc_array[id][0]!='=' && calc_array[id][1]!=1) |
||
62 | { |
||
63 | eval('calcul='+calc_array[id][2]+calc_array[id][0]+calc_array[id][3]+';'); |
||
64 | $id(id+'_result').value=calcul; |
||
65 | calc_array[id][2]=calcul; |
||
66 | calc_array[id][3]=0; |
||
67 | } |
||
68 | calc_array[id][0] = n; |
||
69 | } |
||
70 | if(pas_ch==0) |
||
71 | { |
||
72 | calc_array[id][1] = 1; |
||
73 | } |
||
74 | else |
||
75 | { |
||
76 | pas_ch=0; |
||
77 | } |
||
78 | document.getElementById(id+'_result').focus(); |
||
79 | return true; |
||
80 | } |
||
81 | function add_calc(id,n) |
||
168 | } |